package leetCode;

/**
 * TODO
 *
 * @author 司松坡
 * @createTime 2023年03月07日
 */
public class LeetCode31 {
    public void nextPermutation(int[] nums) {
        if (nums.length <= 1) {
            return;
        }
        int start = nums.length - 2;
        while (start >= 0 && nums[start] >= nums[start + 1]) {
            start--;
        }
        if (start >= 0) {
            int j = nums.length - 1;
            while (j >= 0 && nums[start] >= nums[j]) {
                j--;
            }
            exchangeNum(nums, start, j);
        }
        exchange(nums, start + 1);

    }

    /**
     * @description 交换数组
     * @date 2023/3/7
     */
    public void exchangeNum(int[] nums, int i, int j) {
        int min = nums[i];
        nums[i] = nums[j];
        nums[j] = min;
    }

    /**
     * @description
     * @date 2023/3/7
     */
    public void exchange(int[] nums, int start) {
        int end = nums.length - 1;
        while (start < end) {
            exchangeNum(nums, start, end);
            start++;
            end--;
        }
    }


}
